package com.ex2;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class Client {
    public static void main(String[] args) throws Exception {
        // 创建一个 Socket 对象，指定服务端的 IP 地址和端口号
        Socket socket = new Socket("localhost", 8888);
        // 获取输出流，向服务端发送数据
        OutputStream os = socket.getOutputStream();
        os.write("hello, server".getBytes());
        // 获取输入流，从服务端接收数据
        InputStream is = socket.getInputStream();
        byte[] buffer = new byte[1024];
        int len = is.read(buffer);
        String data = new String(buffer, 0, len);
        System.out.println("client received: " + data);
        // 关闭流和 Socket 连接
        is.close();
        os.close();
        socket.close();
    }
}
